package com.qianyu.project.web.controller.lyz;

import com.qianyu.project.entity.MaterialUseDetail;
import com.qianyu.project.entity.MaterialUsePlan;
import com.qianyu.project.service.impl.MaterialUsePlanServiceImpl;
import com.qianyu.project.utils.Result;
import com.qianyu.project.utils.ResultTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lyz
 * @date create in 2021/2/4 14:48
 */
@RestController
@RequestMapping("/MaterialUsePlan")
public class MaterialUsePlanController {

    @Autowired
    private MaterialUsePlanServiceImpl service;
    /**
     *
     * description:  <br/>
     * author: lyz   <br/>
     * date: 2021/2/4 <br/>
     * params:  <br/>
     * return: 返回成功信息
     */
    @PostMapping("")
    @Transactional
    public Result saveMaterialUsePlan(@RequestBody Map<String,Object> map){

        System.out.println(map);
        Map<String,Object> planMap=(Map<String,Object>)map.get("plan");
        List<Map<String,Object>> listMap= (List<Map<String,Object>>) map.get("details");

        MaterialUsePlan plan=new MaterialUsePlan();
        plan.setApplyer(planMap.get("applyer").toString());
        plan.setDate(planMap.get("date").toString());
        plan.setPlanSubject(planMap.get("planSubject").toString());
        plan.setProId(Integer.parseInt(planMap.get("proId").toString()));
        plan.setComment(planMap.get("comment").toString());
        plan.setApproveStatus(1);
        plan.setFlowId(1);

        List<MaterialUseDetail> details=new ArrayList<MaterialUseDetail>();
        listMap.forEach(d->{
            MaterialUseDetail detail=new MaterialUseDetail();
            detail.setMateId(Integer.parseInt(d.get("mateTypeId").toString()));
            detail.setUseCount(Long.parseLong(d.get("useCount").toString()));
            detail.setUseTo(d.get("useTo").toString());
            detail.setComment(d.get("comment").toString());
            detail.setReferencePrice(new BigDecimal(d.get("price").toString()).multiply(new BigDecimal(detail.getUseCount())));
            details.add(detail);
        });
        service.saveMaterialUsePlan(plan,details);
        return ResultTool.success();
    }

    /**
     *
     * description:  <br/>
     * author: lyz   <br/>
     * date: 2021/2/4 <br/>
     * params:  <br/>页面高级查询条件
     * return: 返回携带查询信息的list
     */
    @GetMapping("")
    public Result getMaterialUsePlanList(@RequestParam Map<String,String> map){
        List list=service.getUsePlanList(map);
        return ResultTool.success(list);
    }

    @DeleteMapping("/{id}")
    public Result deletePlan(@PathVariable String id){
        service.removeById(id);
        return ResultTool.success();
    }

    @GetMapping("/{id}")
    public Result getPlanById(@PathVariable String id){
        return ResultTool.success(service.getPlanById(id));
    }

    @PutMapping("")
    public Result updatePlan(@RequestBody MaterialUsePlan plan){
        service.updateById(plan);
        return ResultTool.success();
    }
}
